package actions;

import java.util.ArrayList;
import java.util.Map;

import javax.ejb.EJB;

import org.apache.struts2.ServletActionContext;

import beans.BookManager;
import beans.BookManagerBean;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import entities.Item;

public class EditBooksAction extends ActionSupport {

	@EJB
	private BookManager bookManager;

	private Item item = new Item();

	private ArrayList<String> cpyStatus = new ArrayList<String>();
	private ArrayList<String> visibility = new ArrayList<String>();

	public String execute() {

		visibility.add(Item.PUBLIC);
		visibility.add(Item.PRIVATE);
		visibility.add(Item.FRIENDSONLY);

		cpyStatus.add(Item.inLib);
		cpyStatus.add(Item.loaned);
		cpyStatus.add(Item.lost);
		cpyStatus.add(Item.unavailable);

		Map session = ActionContext.getContext().getSession();
		Integer id = (Integer) session.get("userId");
		if (id == null) {
			this.addActionError("User ID is null");
			return ERROR;
		}

		bookManager = new BookManagerBean();
		String copyid = ServletActionContext.getRequest()
				.getParameter("bookId");
		int val = Integer.parseInt(copyid.trim());

		item = bookManager.getCopy(val);
		if (item == null)
			return "NoBook";
		return SUCCESS;
	}

	public Item getItem() {
		return this.item;
	}

	public void setItem(Item item) {
		this.item = item;
	}

	public ArrayList<String> getVisibility() {
		return visibility;
	}

	public ArrayList<String> getCpyStatus() {
		return cpyStatus;
	}

}
